import random
from django.http import JsonResponse
from rest_framework.views import  APIView
from database import models
from django.db.models import Sum





class Echarts1(APIView):

    def get(self, request):

        data = {}

        try:
            object_data = models.Total.objects.order_by("-wages")
            name_list = []
            y_list = []
            for obj in object_data[0:5]:
                name_list.append(obj.jobName[0:8])
                y_list.append(obj.wages)

            data['code'] = 200
            data['name_list'] = name_list
            data['y_list'] = y_list

            # 5
            name_5list = []
            y_5list = []
            for obj in object_data:
                if obj.jobDegree not in name_5list:
                    name_5list.append(obj.jobDegree)
            for name in name_5list:
                y_5list.append(models.Total.objects.filter(jobDegree=name).count())

            data['name_5list'] = name_5list
            data['y_5list'] = y_5list



            return JsonResponse(data)

        except Exception as e:
            # print(e)
            data['code'] = 444
            return JsonResponse(data)



